import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class EditText extends StatefulWidget {
  final String labelText;
  final TextInputType keyboardType;
  final bool obscureText;
  final bool showClearIcon;
  final bool isDense;
  final TextEditingController controller;
  final TextCapitalization textCapitalization;
  final TextAlign textAlign;
  final int maxLines;
  final int maxLength;
  final List<TextInputFormatter> inputFormatter;

  final String hintText;
  final TextStyle textStyle;
  final ValueChanged<String> onChanged;
  final Decoration shapeDecoration;
  final EdgeInsetsGeometry padding;
  final EdgeInsetsGeometry margin;

  EditText(
      {this.labelText,
      this.hintText = '',
      this.obscureText: false,
      this.keyboardType: TextInputType.emailAddress,
      this.textAlign = TextAlign.start,
      this.controller,
      this.textStyle,
      this.isDense = true,
      this.padding,
      this.margin,
      this.onChanged,
      this.shapeDecoration,
      this.textCapitalization = TextCapitalization.none,
      this.showClearIcon = false,
      this.maxLines = 1,
      this.maxLength = 20,
      this.inputFormatter});

  @override
  State<StatefulWidget> createState() {
    return _EditTextState();
  }
}

class _EditTextState extends State<EditText> {
  bool iconVisible = false;
  IconData icon;

  void submitter(String text) {
    iconVisible = null != text && text.isNotEmpty;
    updateIcons();
  }

  void updateIcons() {
    setState(() {
      icon = iconVisible ? Icons.close : null;
    });
  }

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle = null == widget.textStyle
        ? TextStyle(color: Colors.black, fontSize: 18.0)
        : widget.textStyle;
    bool showIcon = widget.showClearIcon;

    return showIcon ? _inputClearWidget(textStyle) : _inputWidget(textStyle);
  }

  _inputWidget(TextStyle textStyle) {
    var textField = _field(textStyle, widget.onChanged, null);
    return Container(
      child: textField,
      decoration: widget.shapeDecoration,
      padding: widget.padding,
      margin: widget.margin,
    );
  }

  _inputClearWidget(TextStyle textStyle) {
    var textField = _field(textStyle, (String inputText) {
      iconVisible = null != inputText && inputText.isNotEmpty;
      updateIcons();
    }, submitter);

    return Row(
      children: <Widget>[
        Flexible(
          child: Container(
            margin: widget.margin,
            padding: widget.padding,
            child: textField,
          ),
        ),
        IconButton(
            color: Colors.grey,
            icon: Icon(icon),
            onPressed: () {
              widget.controller.clear();
              submitter('');
            }),
      ],
    );
  }

  TextField _field(
    TextStyle textStyle,
    ValueChanged<String> onChanged,
    ValueChanged<String> onSubmitted,
  ) {
    return TextField(
      maxLength: widget.maxLength,
      maxLines: widget.maxLines,
      textCapitalization: widget.textCapitalization,
      style: textStyle,
      textAlign: widget.textAlign,
      onChanged: onChanged,
      keyboardType: widget.keyboardType,
      inputFormatters: widget.inputFormatter,
      controller: widget.controller,
      obscureText: widget.obscureText,
      onSubmitted: onSubmitted,
      decoration: InputDecoration(
          counterText: '',
          isDense: widget.isDense ?? widget.isDense,
          labelText: widget.labelText,
          hintText: widget.hintText,
          contentPadding: EdgeInsets.zero,
          border: InputBorder.none),
    );
  }
}

class CyxmEditWidget extends StatelessWidget {
  final Key key;
  final ValueChanged<String> onChanged;
  final String hintText;
  final TextEditingController controller;

  const CyxmEditWidget(
      {this.key, this.onChanged, this.hintText, this.controller})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        constraints: BoxConstraints(maxWidth: 80),
        decoration: BoxDecoration(
            border: Border.all(color: Color(0xFF0099CC), width: 1),
            borderRadius: BorderRadius.all(Radius.circular(5))),
        child: EditText(
          padding: EdgeInsets.all(5),
          maxLength: 2,
          hintText: hintText,
          onChanged: onChanged,
          controller: controller,
          keyboardType: TextInputType.number,
          inputFormatter: [WhitelistingTextInputFormatter.digitsOnly],
        ));
  }
}
